package com.taoym.voucher.admin.service.impl;

import com.taoym.voucher.admin.domain.XcxUserInfo;
import com.taoym.voucher.admin.entity.dto.XcxGetUserInfosDTO;
import com.taoym.voucher.admin.entity.vo.XcxGetUserInfosVO;
import com.taoym.voucher.admin.mapper.XcxUserInfoMapper;
import com.taoym.voucher.admin.service.XcxUserInfoService;
import com.taoym.voucher.common.enums.DateConditionEnum;
import com.taoym.voucher.common.utils.SecureUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @coder taoym
 * @datetime 2021/7/1 10:22
 * @desc
 */
@Service
public class XcxUserInfoServiceImpl implements XcxUserInfoService {

    @Resource
    private XcxUserInfoMapper xcxUserInfoMapper;

    @Override
    public XcxGetUserInfosVO listUserInfo(XcxGetUserInfosDTO data) {
        Map<String, LocalDateTime> dateLocalDate = DateConditionEnum.getDateLocalDate(data.getDateType());

        Integer count = xcxUserInfoMapper.getCount(dateLocalDate.get("start"),
                dateLocalDate.get("end"));
        if (count.equals(0)) {
            return null;
        }
        XcxGetUserInfosVO xcxGetUserInfosVO = new XcxGetUserInfosVO();
        xcxGetUserInfosVO.setCount(count);

        List<XcxUserInfo> xcxUserInfos = xcxUserInfoMapper.getUserInfoList((data.getPage() - 1) * data.getSize(),
                data.getSize(),
                dateLocalDate.get("start"),
                dateLocalDate.get("end"));
        List<XcxGetUserInfosVO.GetUserInfo> getUserInfos = new ArrayList<>(xcxUserInfos.size());
        xcxUserInfos.forEach(e -> {
            XcxGetUserInfosVO.GetUserInfo getUserInfo = new XcxGetUserInfosVO.GetUserInfo();
            getUserInfo.setChiefUid(e.getChiefUid());
            getUserInfo.setEmail(e.getEmail());
            getUserInfo.setCreateTime(e.getCreateTime());
            getUserInfo.setId(e.getId());
            getUserInfo.setZfbName(e.getZfbName());
            getUserInfo.setNickName(e.getNickName());
            getUserInfo.setUid(e.getUid());
            getUserInfo.setZfb(SecureUtils.decrypt(e.getZfb()));
            getUserInfos.add(getUserInfo);
        });
        xcxGetUserInfosVO.setGetUserInfos(getUserInfos);
        return xcxGetUserInfosVO;
    }
}
